package com.zcn.io.web.auth.config;

import com.alibaba.fastjson.JSON;
import com.zcn.io.common.pojo.CommonResult;
import com.zcn.io.web.auth.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/10/20 19:51
 * @Copyright (c) 2024,  All rights reserved.
 */

@Component
public class AuthenticationEntryPointConfig implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        CommonResult<Object> commonResult = CommonResult.error(HttpStatus.UNAUTHORIZED.value(), "认证失败请重新登录");
        String json = JSON.toJSONString(commonResult);
        WebUtils.renderString(response, json);
    }
}

